<template>
  <el-dialog :title="`${isEdit ? '编辑' : '新增'}区域`" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false" :before-close="close">
    <div>
      <el-form ref="RefForm" :model="formData" label-width="100px" :validate-on-rule-change="false" :rules="formRules" status-icon>
        <el-form-item label="区域编码" prop="code">
          <el-input v-model="formData.code" maxlength="20" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="区域名称" prop="name">
          <el-input v-model="formData.name" maxlength="20" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="限制人数" prop="limitation">
          <el-input v-model="formData.limitation" maxlength="10" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark" maxlength="200" show-word-limit type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button :loading="loadingSubmit" type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import dialogEditMixin from '@/mixins/dialogEdit'
  import { eleValidator } from '@/utils/tools'
  import { areaMaxId } from '@/api/api'
  export default {
    mixins: [dialogEditMixin],
    data() {
      return {
        maxId: '',
        options: [],
        formRules: {
          code: [
          
            {
              validator: eleValidator([
             
                { rule: 'isRepeat', msg: { value: '区域编码重复', url: '/Area/Exists', key: 'code' } }
              ]), trigger: 'blur'
            }
          ],
          name: [
            { required: true, message: '请输入区域名称', trigger: 'blur' },
            {
              validator: eleValidator([
                { rule: 'isEmpty', msg: '区域名称不能为空' },
                { rule: 'isRepeat', msg: { value: '区域名称重复', url: '/Area/Exists', key: 'name' } }
              ]), trigger: 'blur'
            }
          ],
          limitation: [
            // { required: true, message: '请输入区域名称', trigger: 'blur' },
            {
              validator: eleValidator([
                { rule: 'isNumber', msg: '限制人数只能输入数字' }
              ]), trigger: 'blur'
            }
          ]
        }
      }
    },
    watch: {
      dialogVisible: function (newValue, oldValue) {
        if (newValue) { return }
        this.$refs.RefForm.clearValidate()
      }
    },
    methods: {
      resetFormData() {
        this.formData = {
          code: '',
          name: '',
          limitation: '',
          remark: ''
        }
      },
      // 请求区域ID
      async getAreaMaxId() {
        try {
          let response = await areaMaxId()
          this.maxId = response.data
          this.formData.code = this.maxId
        } catch (err) {
          let er = err
        }
      },
    }
  }
</script>

<style lang="scss" scoped></style>
